package com.sssre.business.service.impl;

import java.util.Date;
import java.util.List;

import com.sssre.business.domain.BusStatement;
import com.sssre.business.exception.ServiceValidException;
import com.sssre.business.mapper.BusStatementMapper;
import com.sssre.business.util.RegexUtils;
import com.sssre.business.util.VehiclePlateNoUtil;
import com.sssre.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sssre.business.mapper.BusAppointmentMapper;
import com.sssre.business.domain.BusAppointment;
import com.sssre.business.service.IBusAppointmentService;

/**
 * 养修信息预约Service业务层处理
 * 
 * @author 浪兎兎
 * @date 2025-04-28
 */
@Service
public class BusAppointmentServiceImpl implements IBusAppointmentService 
{
    @Autowired
    private BusAppointmentMapper busAppointmentMapper;
    @Autowired
    private BusStatementMapper busStatementMapper;

    /**
     * 查询养修信息预约
     * 
     * @param id 养修信息预约主键
     * @return 养修信息预约
     */
    @Override
    public BusAppointment selectBusAppointmentById(Long id)
    {
        return busAppointmentMapper.selectBusAppointmentById(id);
    }

    /**
     * 查询养修信息预约列表
     * 
     * @param busAppointment 养修信息预约
     * @return 养修信息预约
     */
    @Override
    public List<BusAppointment> selectBusAppointmentList(BusAppointment busAppointment)
    {
        return busAppointmentMapper.selectBusAppointmentList(busAppointment);
    }

    /**
     * 新增养修信息预约
     * 
     * @param busAppointment 养修信息预约
     * @return 结果
     */
    @Override
    public int insertBusAppointment(BusAppointment busAppointment)
    {
        // 基础校验
        valid(busAppointment);
        // 设置状态：预约中（0）
        busAppointment.setStatus(BusAppointment.STATUS_BOOKING);
        // 设置创建时间
        busAppointment.setCreateTime(DateUtils.getNowDate());
        // 插入表
        return busAppointmentMapper.insertBusAppointment(busAppointment);
    }
    /**
     * 数据合法性校验
     *
     * @param busAppointment 养修信息预约
    * */
    private void valid(BusAppointment busAppointment) {
        // 数据合法性校验
        // not null
        if (busAppointment == null) {
            throw new ServiceValidException("数据不能为空");
        }
        // 车牌号
        String licensePlate = busAppointment.getLicensePlate();
        VehiclePlateNoUtil.VehiclePlateNoEnum vehiclePlateNo = VehiclePlateNoUtil.getVehiclePlateNo(licensePlate);
        if (vehiclePlateNo == null || vehiclePlateNo.ordinal() >= 7) {
            throw new ServiceValidException("车牌号格式错误");
        }
        // 手机号
        String customerPhone = busAppointment.getCustomerPhone();
        if (!RegexUtils.isChinaPhoneLegal(customerPhone)) {
            throw new ServiceValidException("手机号格式有误");
        }
        // 预约时间
        Date appointmentTime = busAppointment.getAppointmentTime();
        if (appointmentTime.before(new Date())) {
            throw new ServiceValidException("预约时间必须在服务器当前时间之后");
        }

    }

    /**
     * 修改养修信息预约
     * 
     * @param busAppointment 养修信息预约
     * @return 结果
     */
    @Override
    public int updateBusAppointment(BusAppointment busAppointment)
    {
        // 基础校验
        valid(busAppointment);
        // 判断状态是否合法
        // 根据id查询老对象
        Long oldId = busAppointment.getId();
        BusAppointment oldBusAppointment = busAppointmentMapper.selectBusAppointmentById(oldId);
        if (oldBusAppointment == null || !busAppointment.getStatus().equals(oldBusAppointment.getStatus())) {
            throw new ServiceValidException("状态信息不合法");
        }
        // 修改记录
        return busAppointmentMapper.updateBusAppointment(busAppointment);
    }

    /**
     * 批量删除养修信息预约
     * 
     * @param ids 需要删除的养修信息预约主键
     * @return 结果
     */
    @Override
    public int deleteBusAppointmentByIds(Long[] ids)
    {
        // 数据校验
        if (ids == null || ids.length == 0) {
            throw new ServiceValidException("数据不能为空");
        }
        // 删除
        return busAppointmentMapper.deleteBusAppointmentByIds(ids);
    }

    /**
     * 删除养修信息预约信息
     * 
     * @param id 养修信息预约主键
     * @return 结果
     */
    @Override
    public int deleteBusAppointmentById(Long id)
    {
        return busAppointmentMapper.deleteBusAppointmentById(id);
    }

    /**
     * 到店
     *
     * @param id 养修信息预约主键
     * @return 结果
     */
    @Override
    public int arrived(Long id) {
        // 参数合法性验证
        if (id == null) throw new ServiceValidException("数据非法");
        // 必须预约中的用户才能到店
        isBooking(id);
        // 提交
        return busAppointmentMapper.arrived(BusAppointment.STATUS_ARRIVED, new Date(), id);
    }

    /**
     * 验证状态信息是否正确
     *
     * @param id 养修信息预约主键
     */
    private void isBooking(Long id) {
        // 必须预约中的用户才能到店
        // 根据id查询老对象
        BusAppointment oldBusAppointment = busAppointmentMapper.selectBusAppointmentById(id);
        if (oldBusAppointment == null || !oldBusAppointment.getStatus().equals(BusAppointment.STATUS_BOOKING)) {
            throw new ServiceValidException("状态信息不合法：必须到店");
        }
    }

    /**
     * 取消养修信息预约信息
     *
     * @param id 养修信息预约主键
     * @return 结果
     */
    @Override
    public int cancel(Long id) {
        // 参数合法性验证
        if (id == null) throw new ServiceValidException("数据非法");
        // 必须预约中的用户才能取消
        isBooking(id);
        // 提交
        return busAppointmentMapper.updateStatus(BusAppointment.STATUS_USER_CANCELLED, id);
    }

    /**
     * 生成结算单
     *
     * @param id 养修信息预约主键
     * @return 结果
     */
    @Override
    public int generateStatement(Long id) {
        // 参数合法性验证
        if (id == null) {
            throw new ServiceValidException("非法参数");
        }
        // 查询原始对象
        BusAppointment oldBusAppointment = busAppointmentMapper.selectBusAppointmentById(id);
        // 判断是否为空
        if (oldBusAppointment == null) {
            throw new ServiceValidException("预约单不存在");
        }
        // 判断状态合法性
        if (!oldBusAppointment.getStatus().equals(BusAppointment.STATUS_ARRIVED)) {
            throw new ServiceValidException("只有已到店的预约单才能生成结算单");
        }
        // 插入结算单表
        BusStatement busStatement = new BusStatement();
        busStatement.setCustomerName("客服录入");
        busStatement.setCustomerPhone(oldBusAppointment.getCustomerPhone());
        busStatement.setActualArrivalTime(oldBusAppointment.getActualArrivalTime());
        busStatement.setLicensePlate(oldBusAppointment.getLicensePlate());
        busStatement.setCarSeries(oldBusAppointment.getCarSeries());
        busStatement.setServiceType(oldBusAppointment.getServiceType());
        busStatement.setAppointmentId(oldBusAppointment.getId());
        busStatement.setStatus(0);
        busStatement.setIsDelete(0);
        busStatement.setSearchValue("");
        busStatement.setCreateTime(new Date());
        busStatement.setUpdateTime(new Date());
        busStatementMapper.insertBusStatement(busStatement);
        // 修改预约单状态为已结算
        return busAppointmentMapper.updateStatus(BusAppointment.STATUS_SETTLED, id);
    }

    @Override
    public BusStatement getStatement(Long id) {
        // 参数合法性验证
        if (id == null) {
            throw new ServiceValidException("非法参数");
        }
        // 查询原始对象
        BusAppointment oldBusAppointment = busAppointmentMapper.selectBusAppointmentById(id);
        // 判断是否为空
        if (oldBusAppointment == null) {
            throw new ServiceValidException("预约单不存在");
        }
        // 然后根据id查询
        BusStatement busStatement = busStatementMapper.selectByAppointmentId(id);
        if (busStatement.getIsDelete().equals(BusStatement.DELETED)) {
            // 如果数据被删除，恢复数据
            busStatementMapper.undeleteStatement(busStatement.getId());
        }
        return busStatement;
    }
}
